package com.central.apps.controller;

import com.central.app.service.HandlerService;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购订单接口类
 *
 * @author 李大超
 * @date 2022/10/17
 */
@RestController
@RequestMapping("/purchaseOrder")
public class PurchaseOrderController {
    @Autowired
    private HandlerService handlerService;

    @PostMapping("/generateContract")
    public String generate(@RequestBody Map<String,Object> params) throws Exception {
        JSONObject json = new JSONObject();

        //String poid = MapUtils.getString(params,"poid");
        String status = MapUtils.getString(params,"status");
        String id = MapUtils.getString(params,"id");//采购订单主键id

        // 判断流程状态-3:审批通过
        if (!status.equals("已批准")) {
            json.put("code", "1");
            json.put("msg", "流程未审批通过, 请审批通过后再生成合同!");
            return json.toString();
        }

        // 查询采购订单信息
        List<Map<String, Object>> poList = handlerService.findBySql("select * from gq_po where (converttocon = '0' or converttocon is null) and id =" + id);
        String connum = "";
        if (CollectionUtils.isNotEmpty(poList)) {
            Map<String, Object> poMap = poList.get(0);
            // 生成合同信息
            Map<String, Object> consignMap = new HashMap<>();
            Map<String, Object> conapplicationMap = new HashMap<>();
            Map<String, Object> consignsMap = new HashMap<>();
            handlerService.addBizNum("conapplicationApp", "gq_conapplication", conapplicationMap);
            handlerService.addBizNum("consignsApp", "gq_consigns", consignsMap);
            consignMap.put("conid","1001"+MapUtils.getString(conapplicationMap, "conid"));
            //consignMap.put("consignsnum","CDCS"+MapUtils.getString(conapplicationMap, "conid"));
            consignMap.put("consignsnum",MapUtils.getString(consignsMap,"consignsnum"));
            consignMap.put("signingplace","微太中心");

            connum = MapUtils.getString(consignMap, "consignsnum");

            BigDecimal totalcost = null;
            List<Map<String, Object>> list = handlerService.findBySql("select sum(totalcost) as totalcost from gq_posline where poid = '"+id+"'");
            if (CollectionUtils.isNotEmpty(list)) {
                totalcost=(BigDecimal)list.get(0).get("totalcost");
            }

            consignMap.put("conname", poMap.get("description"));
            consignMap.put("contype", "物资采购合同");
            // 总公司、分公司、部门编码空值
            consignMap.put("orgcode", poMap.get("orgcode"));
            consignMap.put("sitecode", poMap.get("sitecode"));
            consignMap.put("deptcode", poMap.get("deptcode"));
            //consignMap.put("createuser", poMap.get("createuser"));
            //consignMap.put("createuserdesc", poMap.get("createuserdesc"));
            consignMap.put("ponum", poMap.get("ponum"));//采购订单编号
            consignMap.put("poname", poMap.get("description"));
            consignMap.put("supplierno", poMap.get("suppliernum"));
            consignMap.put("oppcompany", poMap.get("suppliername"));
            consignMap.put("status", "新建");
            consignMap.put("constatus", "起草");
            consignMap.put("contracttype", "物资采购合同");
            consignMap.put("totalprice", totalcost);//合同金额
            consignMap.put("nototalprice", totalcost);//合同金额
            consignMap.put("poid", id);//采购订单编号
            consignMap.put("isframecon", "否");//是否框架合同

            consignMap.put("iscollcon", "付款合同");//设置付款合同类型
            handlerService.insert("gq_consigns", consignMap);
            //同时添加到台账
            handlerService.insert("gq_conaccount", consignMap);
            // 更新采购订单标记信息
            handlerService.updateBySql("update gq_po set converttocon = '1',issetcon='是' where id = " + id);
            //将采购订单行数据，同步到合同签订页面，给采购订单行数据
            handlerService.updateBySql("update gq_poline set conid="+consignMap.get("conid") +"  where poid = "+id);
        } else {
            json.put("code", "1");
            json.put("msg", "已经生成合同, 请仔细核对信息!");
            return json.toString();
        }
        json.put("code", "0");
        json.put("msg", "生成合同成功, 请到合同签订界面查询合同签订编号为\""+connum+"\"的数据!");
        return json.toString();
    }
}
